package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.SPlanKindManager;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by wangyanan on 2016/8/29.
 * 工作类别
 */
public class PlanKindListFragment extends BaseListFragment {
  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.extra_value";

  private EditText mSearchEt;

  private ArrayList<SPlanKindManager.PlanKind> mList;
  private PlanKindAdapter mAdapter;

  public static PlanKindListFragment newInstance() {

    return new PlanKindListFragment();
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    ActionBar actionBar = mActivity.getSupportActionBar();
    if (null != actionBar) {
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setTitle(R.string.work_type);
    }
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mListview.setDivider(null);
    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_search_person, null, false);
    mSearchEt = (EditText) header.findViewById(R.id.et_search_searchPersonHeader);
    mSearchEt.setHint(R.string.search_work_type);
    mSearchEt.addTextChangedListener(new TextWatcher() {

      @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (mList == null || mList.size() == 0) {
          return;
        }
        String query = s.toString().trim();
        if (query == null || query.equals("")) {
          setAdapter(mList);
        } else {
          updateItems(query);
        }

        mSearchEt.requestFocus();
      }

      @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Auto-generated method stub

      }

      @Override public void afterTextChanged(Editable s) {
        // Auto-generated method stub

      }
    });

    mListview.addHeaderView(header);
  }

  @Override public String getUrl() {
    // Auto-generated method stub
    return ApiConst.URL_PLAN＿KIND;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<>();
    params.put("curPage", "1");
    params.put("pageSize", "1000");
    return params;
  }

  @Override public void resolveResponse(String json) {
    SPlanKindManager messageDialogs = new Gson().fromJson(json, SPlanKindManager.class);
    if (messageDialogs != null) {
      mList = messageDialogs.getDataList();
    }

    setAdapter(mList);
  }

  private void setAdapter(ArrayList<SPlanKindManager.PlanKind> detail) {

    if (mList == null) {
      mList = new ArrayList<>();
    }
    mAdapter = new PlanKindAdapter(getActivity(), detail);
    setListAdapter(mAdapter);
    ((PlanKindAdapter) getListAdapter()).notifyDataSetChanged();
  }

  public void updateItems(String query) {

    ArrayList<SPlanKindManager.PlanKind> listNames = new ArrayList<>();
    String str;
    for (int i = 0; i < mList.size(); i++) {
      str = mList.get(i).getPlanKindName();
      if (str.contains(query)) {
        listNames.add(mList.get(i));
      }
    }
    setAdapter(listNames);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    SPlanKindManager.PlanKind item = mAdapter.getItem(position - 2);
    CustomerDialog dialog = new CustomerDialog(item.getPlanKindName(), item.getPlanKindCode());
    Intent i = new Intent();
    i.putExtra(EXTRA_VALUE, dialog);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:
        getActivity().finish();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }
}
